<template>
	<view class="container">
		<view class="tui-page-title">重设密码</view>
		<view class="tui-form">
			<view class="tui-view-input">
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="pwd" color="#4d2574" :size="20"></tui-icon>
						<input :value="password" placeholder="请输入六位及以上的密码" :password="true" placeholder-class="tui-phcolor" type="text" maxlength="40" @input="inputPwd" />
						<view class="tui-icon-close" v-show="password" @tap="clearInput(2)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				<tui-list-cell :hover="false" :lineLeft="false" backgroundColor="transparent">
					<view class="tui-cell-input">
						<tui-icon name="pwd" color="#4d2574" :size="20"></tui-icon>
						<input :value="password2" placeholder="请再次确认密码" :password="true" placeholder-class="tui-phcolor" type="text" maxlength="40" @input="inputPwd2" />
						<view class="tui-icon-close" v-show="password2" @tap="clearInput(3)"><tui-icon name="close-fill" :size="16" color="#bfbfbf"></tui-icon></view>
					</view>
				</tui-list-cell>
				
			</view>
			<view class="tui-btn-box"><tui-button :disabledGray="true" :disabled="disabled" :shadow="true" shape="circle" @tap="retPwd">保存</tui-button></view>
			
		</view>
	</view>
</template>

<script>
const form = require("@/common/validation.js")
import tuiButton from "@/components/tui/tui-button"
import tuiIcon from "@/components/tui/tui-icon"
import tuiListCell from "@/components/tui/tui-list-cell"	
let time = Date.parse(new Date()) / 1000
export default {
	computed: {
		disabled: function() {
			let bool = true;
			if (this.password2 && this.password) {
				bool = false;
			}
			return bool;
		}
	},
	components:{
		tuiButton,
		tuiIcon,
		tuiListCell,
		
	},
	data() {
		return {
			
			password: '',
			password2: '',
			isSend: false,
			codeTime:0,
			color:"#ffffff",
			background:"#a778d4",
			post_data:{}
		};
	},
	onLoad() {
		let token = uni.getStorageSync('token');
		if(!token){
			setTimeout(() => {
				uni.showToast({
					title:'您尚未登录',
					icon:"none"
				})
				uni.redirectTo({
					url: './login'
				})
				
			}, 2000);
			
		}
		
	},
	methods: {
		
		inputPwd: function(e) {
			this.password = e.detail.value;
			console.log(this.password)
		},
		inputPwd2: function(e) {
			this.password2 = e.detail.value;
			console.log(this.password2)
		},
		clearInput(type) {
		    if(type == 2)
			   this.password = '';
			else {
				this.password2 = '';
			}
		},
		retPwd(){
			let that = this
			let post_data = {pwd:that.password,pwd2:that.password2}
			
			//表单规则
			let rules = [ 
			{
				name: "pwd",
				rule: ["required", "isEnAndNo"],
				msg: ["请输入密码", "密码为6~20位数字和字母组合"]
			}, {
				name: "pwd2",
				rule: ["required", "isSame:pwd"],
				msg: ["请输入确认密码", "两次输入的密码不一致"]
			},
			];
			//进行表单检查
			let checkRes = form.validation(post_data, rules);
			if (!checkRes) {
				that.$H.post('sapi/auth/retPwd',post_data).then(res => {
					
					if(res.status == 200){
						setTimeout(() => {
							uni.showToast({
								title:"密码修改成功",
								icon:"none"
							})
							
						}, 1500);
						uni.switchTab({
							url: '/pages/user/user'
						})
						
					}else{
						setTimeout(() => {
							uni.showToast({
								title:res.msg,
								icon:"none"
							})
							
						}, 1000);
					}
						
							
					})
				
			} else {
				uni.showToast({
					title: checkRes,
					icon: "none"
				});
			}
			
			
		},
		
	}
};
</script>

<style lang="scss" scoped>
.container {
	.tui-page-title {
		width: 100%;
		font-size: 48rpx;
		font-weight: bold;
		color: $uni-text-color;
		line-height: 42rpx;
		padding: 110rpx 40rpx 40rpx 40rpx;
		box-sizing: border-box;
		text-align: center;
	}
	.tui-form {
		padding-top: 50rpx;
		.tui-view-input {
			width: 100%;
			box-sizing: border-box;
			padding: 0 40rpx;
			.tui-cell-input {
				width: 100%;
				display: flex;
				align-items: center;
				padding-top: 10rpx;
				padding-bottom: $uni-spacing-col-base;
				input {
					flex: 1;
					padding-left: $uni-spacing-row-base;
				}
				.tui-icon-close {
					margin-left: auto;
				}
				.tui-btn-send {
					width: 156rpx;
					height: 50rpx;
					text-align: center;
					padding-top: 10rpx;
					flex-shrink: 0;
					font-size: $uni-font-size-base;
					color: #ffffff;
					background: $uni-text-color;//
				}
				.tui-gray {
					//color: $uni-text-color-placeholder;
					background: #c09ee0;
					padding-top: 20rpx;
				}
			}
		}
		.tui-cell-text {
			width: 100%;
			padding: 40rpx $uni-spacing-row-lg;
			box-sizing: border-box;
			font-size: $uni-font-size-sm;
			color: $uni-text-color-grey;
			display: flex;
			align-items: center;
			.tui-color-primary {
				color: #4d2574;
				padding-left: $uni-spacing-row-sm;
			}
		}
		.tui-btn-box {
			width: 100%;
			padding: 0 $uni-spacing-row-lg;
			box-sizing: border-box;
			margin-top: 80rpx;
		}
	}
}
</style>
